package com.sc.site.goods;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.sc.dal.common.pagination.PaginationBean;
import com.sc.dal.mapper.goods.GoodsMapper;
import com.sc.dal.model.goods.Goods;
import com.sc.dal.model.goods.GoodsClass;
import com.sc.service.goods.GoodsClassService;
import com.sc.service.goods.GoodsService;
import com.sc.service.promotion.PromotionService;
import com.sc.util.exception.ScBizException;


@RequestMapping(value="goodsClass")
@Controller
public class GoodsClassController {
	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private GoodsService goodsService;
	@Autowired
	private GoodsClassService goodsClassService;
	@Autowired
	private PromotionService promotionService;
	
	
	/***
	 * 导航----全部商品查询------列表信息
	 * 分页查询
	 * Integer currPage  当前页数
	 * Integer pageSize  查询条数
	 * 根据分类ID查询商品列表信息
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/queryGoodsClassList")
	@ResponseBody
	public Object queryGoodsClassList(HttpServletRequest request,Integer currPage, Integer pageSize){
		String classId = request.getParameter("classId");
		String className = request.getParameter("className");
		List<Map<String, Object>> resultList = null;
		int totalRecords = 0;
		int totalPage = 0;
		pageSize=16;
		ModelAndView mav = new ModelAndView("/goodsClass/pro_list");
		Map<String, Object> paramMap = new LinkedHashMap<String, Object>();
		Map<String, Object> paramHotMap = new LinkedHashMap<String, Object>();
		if(StringUtils.isNotBlank(classId)){
			paramMap.put("classId", classId);
			mav.addObject("classId", classId);
			request.setAttribute("className", className);
		}else{
			mav.addObject("classId", "null");
		}
		String searchName = request.getParameter("searchName");
		if(StringUtils.isNotBlank(searchName)){
			paramMap.put("searchName", searchName);
			mav.addObject("searchName", searchName);
		}/*else{
			mav.addObject("searchName", "");
		}*/
//		List<Goods> goodsList = goodsService.queryGoodsByClassIdList(paramMap);
		try{
			//根据分类ID搜索商品
//			PaginationBean<Map<String,Object>> paginationBean = this.goodsService.getPagingDatas(paramMap, currPage, pageSize, "queryGoodsByClassIdCount", "queryGoodsByClassIdList", GoodsMapper.class);
			PaginationBean<Map<String,Object>> paginationBean = this.goodsService.getPagingDatas(paramMap, currPage, pageSize, "count", "selectList", GoodsMapper.class);
			resultList = paginationBean.getPageList();
			for (int i = 0; i < resultList.size(); i++) {
				Goods goods = (Goods) resultList.get(i);
				Integer id = goods.getId();
				String goodsType = goods.getType();

				//***获取该商品的月销量****************//
				int monthSaleMount =  goodsService.selectGoodsMonthSaleMount(id);	
				goods.setMonthSaleMount(String.valueOf(monthSaleMount));
				Map<String, Object> map = new LinkedHashMap<String, Object>();
				map.put("goodsId", id);
				String proTitle="";
				if(goodsType!=null){
					String goodstypeTemp[] = goodsType.split("-");
					for(int j =0;j<goodstypeTemp.length;j++ ){
						if("09".equals(goodstypeTemp[j])){
							proTitle="包邮、";
						}
					}
				}
				proTitle += promotionService.getGoodsByPromtion(map);
				/**  截取字段*/
				if(proTitle.endsWith("、")){
					proTitle = proTitle.substring(0, proTitle.length()-1);
				}
				goods.setProTitle(proTitle);
			}
			totalRecords = paginationBean.getTotalRecords();
			totalPage = paginationBean.getTotalPage();
			paramHotMap.put("type", "04");
			//搜索热卖推荐商品
//			PaginationBean<Map<String,Object>> paginationHot = this.goodsService.getPagingDatas(paramHotMap, currPage, pageSize, "queryGoodsByClassIdCount", "queryGoodsByClassIdList", GoodsMapper.class);
			PaginationBean<Map<String,Object>> paginationHot = this.goodsService.getPagingDatas(paramHotMap, currPage, pageSize, "count", "selectList", GoodsMapper.class);
			mav.addObject("goodsHotList", paginationHot.getPageList());
			
			int currentPage = paginationBean.getCurrentPage();
			mav.addObject("goodsList", resultList);
			mav.addObject("totalRecords", totalRecords);
			mav.addObject("totalPage", totalPage);
			mav.addObject("currentPage", currentPage);
			logger.info("分页查找商品成功！");
		}catch(Exception e){
			logger.error("分页查找商品失败！"+e);
			throw new ScBizException("分页查找商品错误！", e);
		}
		
		return mav;
	}
	
	
	
	@RequestMapping(value="/queryByGoodsClassList")
	@ResponseBody
	public List<GoodsClass> queryByGoodsClassList() {
		List<GoodsClass> GoodsClassList = goodsClassService
				.selectByGoodsClass();
		//request.setAttribute("GoodsClassList", GoodsClassList);
		return GoodsClassList;
		
	}
	
	
	

}
